package com.lizhaoblog.pro006scheduled.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.lizhaoblog.pro006scheduled.service.UserService;
 
@Configuration
public class CxfConfig {
	
	@Value("${webservices.service.prefix}")
	private String prefix;
	@Value("${webservices.username}")
	private String username;
	@Value("${webservices.password}")
	private String password;
    @Autowired
    private UserService userService;
	/**
	 * 作用：改变服务名的前缀名
	 * 此方法被注释后：wsdl访问（默认）地址为http://127.0.0.1:8080/services/user?wsdl
	 * 去掉注释后：wsdl访问地址为：http://127.0.0.1:8080/mySoap/user?wsdl
	 * @return
	 */
	@SuppressWarnings("all")
	@Bean
	public ServletRegistrationBean dispatcherServletForWebService() {
		return new ServletRegistrationBean(new CXFServlet(), prefix + "/*");
	}
	
	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springbus() {
		return new SpringBus();
	}
	
	
	/**
	 * JAX-WS：EndpointImpl
	 * 站点服务：终端路径
	 * @return
	 */
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpointImpl = new EndpointImpl(springbus(), userService);
		// 服务端添加自定义拦截器：用户密码
		endpointImpl.getInInterceptors().add(new AuthInterceptor(username, password));
		endpointImpl.publish("/user");
		return endpointImpl;
	}
 
}